<?php

class Admin_TypecreditController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $typeCredits = new Application_Model_Dao_TypeCredit();
        $this->view->typeCredits = $typeCredits->getAll()->toArray();
    }

    public function addAction(){
    	
    	$form = new Application_Form_TypeCredit();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nowy rodzaj zaliczenia');
		$this->view->TypeCreditForm = $form;
   	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$typeCredit = new Application_Model_Entities_TypeCredit();
    			$typeCredit->setDesignation( $form->getValue('designation'));
    			$typeCreditDao = new Application_Model_Dao_TypeCredit();
    			$typeCreditDao->saveTypeCredit($typeCredit);
    		
    			$this->_helper->redirector('index');		
    		}
    	}
    }
public function deleteAction(){
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$typeCredit = new Application_Model_Entities_TypeCredit();
				$typeCredit->setId($id);
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$typeCreditDao->deleteTypeCredit($typeCredit);
				
			}
			$this->_helper->redirector('index');
		}
		if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$result = $typeCreditDao->getRowTypeCredit($id);
				$this->view->typeCredits = $result;
			}
		}
		if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
				$typeCredit = new Application_Model_Entities_TypeCredit();
				$typeCredit->setId($id);
				$typeCreditDao = new Application_Model_Dao_TypeCredit();
				$typeCreditDao->deleteTypeCredit($typeCredit);
			//$this->_helper->redirector('index');
		}
	}
    
}